/**
 * 研发项目状态业务逻辑
 */
// 直接导入相关模块
const ProjectHelpers = require('../../../config/project_helpers_config');
const cloudHelper = ProjectHelpers.cloudHelper;
const pageHelper = ProjectHelpers.pageHelper;
const ResearchProjectModel = require('../../model/project_model.js');

/**
 * 项目状态业务逻辑
 */
const ProjectStateBiz = {

	/**
	 * 获取下一步可选状态
	 * @param {Object} project 项目数据
	 * @returns {Array} 状态选项
	 */
	getNextStateOptions(project) {
		if (!project) return [];
		
		const currentStatus = project.PROJECT_STATUS;
		const options = [];
		
		switch (currentStatus) {
			case ResearchProjectModel.STATUS.WAIT_APPLY:
				options.push({ label: '申请中', value: ResearchProjectModel.STATUS.APPLYING });
				break;
			case ResearchProjectModel.STATUS.APPLYING:
				options.push({ label: '活动中', value: ResearchProjectModel.STATUS.ACTIVE });
				options.push({ label: '已取消', value: ResearchProjectModel.STATUS.CANCELLED });
				break;
			case ResearchProjectModel.STATUS.HIBERNATING:
				options.push({ label: '活动中', value: ResearchProjectModel.STATUS.ACTIVE });
				options.push({ label: '已取消', value: ResearchProjectModel.STATUS.CANCELLED });
				break;
			case ResearchProjectModel.STATUS.ACTIVE:
				options.push({ label: '休眠中', value: ResearchProjectModel.STATUS.HIBERNATING });
				options.push({ label: '已完成', value: ResearchProjectModel.STATUS.COMPLETED });
				break;
			case ResearchProjectModel.STATUS.COMPLETED:
				options.push({ label: '活动中', value: ResearchProjectModel.STATUS.ACTIVE });
				break;
			case ResearchProjectModel.STATUS.CANCELLED:
				options.push({ label: '申请中', value: ResearchProjectModel.STATUS.APPLYING });
				break;
		}
		
		return options;
	},

	/**
	 * 获取下一步可选活动状态
	 * @param {Object} project 项目数据
	 * @returns {Array} 活动状态选项
	 */
	getNextActiveStateOptions(project) {
		if (!project) return [];
		
		const options = [];
		const currentActiveStatus = project.ACTIVE_STATUS || -1;
		
		// 添加所有活动状态选项
		for (const key in ResearchProjectModel.ACTIVE_STATUS) {
			const value = ResearchProjectModel.ACTIVE_STATUS[key];
			options.push({
				label: ResearchProjectModel.ACTIVE_STATUS_NAME[value],
				value
			});
		}
		
		return options;
	},

	/**
	 * 检查状态流转是否合法
	 * @param {Object} project 项目数据
	 * @param {Number} targetStatus 目标状态
	 * @param {Number} targetActiveStatus 目标活动状态
	 * @returns {Boolean} 是否合法
	 */
	checkStateTransition(project, targetStatus, targetActiveStatus) {
		if (!project) return false;
		
		const currentStatus = project.PROJECT_STATUS;
		
		// 检查状态流转是否合法
		switch (currentStatus) {
			case ResearchProjectModel.STATUS.WAIT_APPLY:
				return targetStatus === ResearchProjectModel.STATUS.APPLYING;
			case ResearchProjectModel.STATUS.APPLYING:
				return targetStatus === ResearchProjectModel.STATUS.ACTIVE || 
					targetStatus === ResearchProjectModel.STATUS.CANCELLED;
			case ResearchProjectModel.STATUS.HIBERNATING:
				return targetStatus === ResearchProjectModel.STATUS.ACTIVE || 
					targetStatus === ResearchProjectModel.STATUS.CANCELLED;
			case ResearchProjectModel.STATUS.ACTIVE:
				return targetStatus === ResearchProjectModel.STATUS.HIBERNATING || 
					targetStatus === ResearchProjectModel.STATUS.COMPLETED;
			case ResearchProjectModel.STATUS.COMPLETED:
				return targetStatus === ResearchProjectModel.STATUS.ACTIVE;
			case ResearchProjectModel.STATUS.CANCELLED:
				return targetStatus === ResearchProjectModel.STATUS.APPLYING;
			default:
				return false;
		}
	},

	/**
	 * 更新项目状态
	 * @param {String} projectId 项目ID
	 * @param {Number} targetStatus 目标状态
	 * @param {Number} targetActiveStatus 目标活动状态
	 * @param {Object} flowData 流转数据
	 * @returns {Promise} 异步结果
	 */
	async updateProjectStatus(projectId, targetStatus, targetActiveStatus, flowData = {}) {
		if (!projectId) return null;
		
		try {
			const params = {
				id: projectId,
				status: targetStatus,
				activeStatus: targetActiveStatus,
				flowData
			};
			
			return await cloudHelper.callCloudData('research/status_update', params);
		} catch (err) {
			console.error('更新项目状态失败', err);
			return null;
		}
	},

	/**
	 * 变更项目状态
	 * @param {Object} project 项目数据
	 * @param {Number} newState 新状态
	 * @returns {Promise} 异步结果
	 */
	async changeProjectState(project, newState) {
		if (!project || newState === undefined) return false;

		try {
			const params = {
				id: project._id,
				state: newState
			};

			await cloudHelper.callCloudData('research_project/state_change', params);
			return true;
		} catch (err) {
			console.error('变更项目状态失败', err);
			pageHelper.showModal('操作失败', '变更项目状态失败: ' + err.message);
			return false;
		}
	},

	/**
	 * 变更项目活跃状态
	 * @param {Object} project 项目数据
	 * @param {Number} newActiveState 新活跃状态
	 * @returns {Promise} 异步结果
	 */
	async changeProjectActiveState(project, newActiveState) {
		if (!project || newActiveState === undefined) return false;

		try {
			const params = {
				id: project._id,
				activeState: newActiveState
			};

			await cloudHelper.callCloudData('research_project/active_change', params);
			return true;
		} catch (err) {
			console.error('变更项目活跃状态失败', err);
			pageHelper.showModal('操作失败', '变更项目活跃状态失败: ' + err.message);
			return false;
		}
	}
};

module.exports = ProjectStateBiz; 